package com.yxdj.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yxdj.common.core.domain.R;
import com.yxdj.common.core.web.controller.BaseController;
import com.yxdj.common.core.web.domain.AjaxResult;
import com.yxdj.common.core.web.page.TableDataInfo;
import com.yxdj.common.log.annotation.Log;
import com.yxdj.common.log.enums.BusinessType;
import com.yxdj.common.security.annotation.InnerAuth;
import com.yxdj.common.security.annotation.RequiresPermissions;
import com.yxdj.service.api.domain.MainCategory;
import com.yxdj.service.service.MainCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 主分类管理
 */
@Tag(name = "主分类管理")
@RestController
@RequestMapping("/main")
public class MainCategoryController extends BaseController {

    @Autowired
    private MainCategoryService mainCategoryService;

    @Operation(summary = "获取所有主分类")
    @GetMapping("/all")
    public AjaxResult getAll() {
        return success(mainCategoryService.list());
    }

    /**
     * 分页获取主分类列表
     */
    @Operation(summary = "分页获取主分类列表")
    @GetMapping("/list")
    public TableDataInfo list(MainCategory mainCategory) {
        startPage();
        LambdaQueryWrapper<MainCategory> mainCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        mainCategoryLambdaQueryWrapper.like(mainCategory.getMainCategoryName() != null,MainCategory::getMainCategoryName,mainCategory.getMainCategoryName());
        List<MainCategory> list = mainCategoryService.list(mainCategoryLambdaQueryWrapper);
        return getDataTable(list);
    }

    /**
     * 获取主分类详细信息
     */
    @Operation(summary = "获取主分类详细信息")
    @GetMapping(value = "/getById/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(mainCategoryService.getById(id));
    }

    /**
     * 新增主分类
     */
    @Operation(summary = "新增主分类")
    @Log(title = "主分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MainCategory mainCategory) {
        return toAjax(mainCategoryService.save(mainCategory));
    }

    /**
     * 修改主分类
     */
    @Operation(summary = "修改主分类")
    @Log(title = "主分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MainCategory mainCategory) {
        return toAjax(mainCategoryService.updateById(mainCategory));
    }

    /**
     * 删除主分类
     */
    @Operation(summary = "删除主分类")
    @RequiresPermissions("system:mainCategory:remove")
    @Log(title = "主分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(mainCategoryService.removeByIds(ids));
    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用获取前几个主分类的方法")
    @GetMapping("/app/getMainCategoryList/{limit}")
    public R<List<MainCategory> > getMainCategoryList(@PathVariable(name = "limit") Integer limit){
        List<MainCategory> mainCategoryList = mainCategoryService.getMainCategoryList(limit);
        return R.ok(mainCategoryList);
    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用获取所有分类的方法")
    @GetMapping("/app/getAllCategory")
    R<List<MainCategory>> getAllCategory(){
        List<MainCategory> mainCategoryList = mainCategoryService.getAllCategory();
        return R.ok(mainCategoryList);
    }
}
